package com.ys.controller.api.advanced;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ys.entity.advanced.AdsSpCampaigns;
import com.ys.entity.advanced.AdsSpCampaignsPlacement;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 广告位报表
 */
@Controller
@RequestMapping("/api/inter/adsSpCampaignsPlacement")
public class ApiInterAdsSpCaPlacementController {
	private static Long MENU_ID = 114L; //


	@Autowired
	private CommonApiService<AdsSpCampaignsPlacement> commonApiService;

	@RequiresPermissions("adsSpCampaignsPlacement:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute AdsSpCampaignsPlacement cond,String conditions){
		cond.setOrderby("A.Date DESC,A.ShopGuid ASC");
		cond = commonApiService.dataAuth(MENU_ID, cond);

		LayuiTableVoJA vo = commonApiService.searchLay(cond);
		{
			JSONObject JO = new JSONObject();
			//金额累计
			Map<String, Object> map = cond.setFields("ISNULL(SUM(A.Impressions),0) AS impressions," +
					" ISNULL(SUM(A.CampaignBudgetAmount),0) AS campaignBudgetAmount," +
					" ISNULL(SUM(A.Clicks),0) AS clicks," +
					" CASE WHEN ISNULL(SUM(A.Impressions),0) = 0 THEN ISNULL(SUM(A.Impressions),0) ELSE Round(ISNULL(SUM(A.Clicks),0)/ISNULL(SUM(A.Impressions),0),2) END AS clickThruRate," +
					" ISNULL(SUM(A.Cost),0) AS cost," +
					" ISNULL(SUM(A.Spend),0) AS spend," +
					" CASE WHEN ISNULL(SUM(A.Clicks),0) = 0 THEN ISNULL(SUM(A.Clicks),0) ELSE Round(ISNULL(SUM(A.Cost),0)/ISNULL(SUM(A.Clicks),0),2) END AS costPerClick," +
					" ISNULL(SUM(A.UnitsSoldClicks1d),0) AS unitsSoldClicks1d," +
					" ISNULL(SUM(A.Sales1d),0) AS sales1d," +
					" ISNULL(SUM(A.Purchases1d),0) AS purchases1d," +
					" ISNULL(SUM(A.UnitsSoldSameSku1d),0) AS unitsSoldSameSku1d," +
					" ISNULL(SUM(A.AttributedSalesSameSku1d),0) AS attributedSalesSameSku1d," +
					" ISNULL(SUM(A.PurchasesSameSku1d),0) AS purchasesSameSku1d"

			)
					.queryMapNoOrder().get(0);
			JO = JSON.parseObject(JSON.toJSONString(map));
			vo.setTotalRow(JO);
		}
		return vo;
	}

	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute AdsSpCampaignsPlacement cond, String conditions) throws Exception{
		cond = commonApiService.dataAuth(MENU_ID, cond);
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.setOrderby("A.Date DESC,A.ShopGuid ASC").queryMap();

		List<YsCol> listField = new YsCol()
				.setMenuId(MENU_ID)
				.setFrontShow(YsColEnum.FrontShow.SHOW.value)
				.setOrderby("A.SortNo asc")
				.queryList();


		ExportExcelUtils<AdsSpCampaignsPlacement> util = new ExportExcelUtils<AdsSpCampaignsPlacement>(new AdsSpCampaignsPlacement());

		String toFilePath = util.exportExcel(listData, listField, "广告位报表");
		UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
		return R.ok(dto.getUrl());
	}
}
